package ns2;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Map;
import java.util.Vector;

import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamConstants;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;

/**
 * class for reading vehicle information
 * @author Thimor Bohn <bohn@itm.uni-luebeck.de>
 *
 */
public class VehicleReader {
	
	public static void read(
			String trace, 
			Map<String, Vector<NSMobilityItem>> info,
			List<Edge> edges) 
	{
		try {
			InputStream in = new FileInputStream(trace);
			XMLInputFactory factory = XMLInputFactory.newInstance();
			XMLStreamReader parser = factory.createXMLStreamReader(in);
			
			float time = -1;
			String lid = "";
			String eid = "";
			String vid = "";
			float pos = 0;
			float speed = 0;
			
			// parse trace file
			for (int event = parser.next(); event != XMLStreamConstants.END_DOCUMENT; event = parser.next()) 
			{
				if (event == XMLStreamConstants.START_ELEMENT) {
					// get current time
					if (parser.getLocalName().equals("timestep")) 
					{
						for (int attr = 0; attr < parser.getAttributeCount(); attr++) {
							String attrName = parser.getAttributeLocalName(attr);
							String value = parser.getAttributeValue(attr);
							if ("time".equals(attrName)) {
								time = Float.parseFloat(value);
							}
						}
					}
					
					if (parser.getLocalName().equals("edge")) 
					{
						for (int attr = 0; attr < parser.getAttributeCount(); attr++) {
							String attrName = parser.getAttributeLocalName(attr);
							String value = parser.getAttributeValue(attr);
							if ("id".equals(attrName)) {
								eid = value;
							}
						}
					}
					
					if (parser.getLocalName().equals("lane")) 
					{
						for (int attr = 0; attr < parser.getAttributeCount(); attr++) {
							String attrName = parser.getAttributeLocalName(attr);
							String value = parser.getAttributeValue(attr);
							if ("id".equals(attrName)) {
								lid = value;
							}
						}
					}
					
					// vehicle element found
					if (parser.getLocalName().equals("vehicle")) 
					{

						for (int attr = 0; attr < parser.getAttributeCount(); attr++) {
							String attrName = parser.getAttributeLocalName(attr);
							String value = parser.getAttributeValue(attr);
							if ("id".equals(attrName)) 
							{
								vid = value;
							}
							if ("speed".equals(attrName)) 
							{
								speed = Float.parseFloat(value);
							}
							if ("pos".equals(attrName)) 
							{
								pos = Float.parseFloat(value);
							}
						}
						// init vector if necessary
						if (! info.containsKey(vid))
						{
							Vector<NSMobilityItem> items = new Vector<NSMobilityItem>();
							info.put(vid, items);
						}
						
						// print out here
						//System.out.println("time = " + time + " edge = " + eid + " lane = " + lid + " vehicle = " + vid + " pos = " + pos + " speed = " + speed);
						// Convert relative pos to absolute pos.
						// 1. find the edge
						for (Edge e : edges)
						{
							if (e.id.equals(eid))
							{
								// 2. find the lane
								Lane l = e.lanes.get(lid);
								if (l != null)
								{
									// 3. calculate the pos
									float xy[] = l.getPositionOnLane(pos);
									
									// 4. new class
									NSMobilityItem mi = new NSMobilityItem((int)time, 0, xy[0], xy[1], speed);
									
									// put to vector
									info.get(vid).add(mi);
									//System.out.println("x = " + xy[0] + " y = " + xy[1]);
								}
							}
						}
						
						
					}
				}
			}
			parser.close();
		} catch (XMLStreamException ex) {
			System.err.println(ex);
		} catch (IOException ex) {
			System.err.println(ex);
		}
	}
	
}
